package com.crm.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.base.ResponseResult;
import com.crm.domain.entity.Clue;
import com.crm.domain.entity.Transfer;
import com.crm.domain.query.TransferQuery;
import com.crm.mapper.ClueMapper;
import com.crm.mapper.TransferMapper;
import com.crm.service.IClueService;
import com.crm.service.ITransferService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 转派管理表 服务实现类
 * </p>
 *
 * @author Hulunico
 * @since 2024-08-09
 */
@Service
@RequiredArgsConstructor
public class TransferServiceImpl extends ServiceImpl<TransferMapper, Transfer> implements ITransferService {

    @Override
    public Page<Transfer> queryAll(TransferQuery query) {
        Page<Transfer> page = ObjectUtil.isEmpty(query.getPageNum())?Page.of(1, 10):Page.of(query.getPageNum(), query.getPageSize());
        Map<String, Object> params = query.getParams();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        LocalDate beginCreateTime =ObjectUtil.isEmpty(params.get("beginTime"))?null:(LocalDate.parse((String)params.get("beginTime"), formatter));
        LocalDate endCreateTime =ObjectUtil.isEmpty(params.get("endTime"))?null:(LocalDate.parse((String)params.get("endTime"), formatter));

        LambdaQueryWrapper<Transfer> wrapper = Wrappers.<Transfer>lambdaQuery()
                .eq(ObjectUtil.isNotEmpty(query.getUserId()),Transfer::getUserId,  query.getUserId())
                .eq(ObjectUtil.isNotEmpty(query.getPhonenumber()),Transfer::getPhonenumber,  query.getPhonenumber())
                .like(ObjectUtil.isNotEmpty(query.getUserName()),Transfer::getUserName, query.getUserName())
                .between(ObjectUtil.isNotEmpty(beginCreateTime)&&ObjectUtil.isNotEmpty(endCreateTime)
                        ,Transfer::getCreateTime, beginCreateTime, endCreateTime);

        //2.2用m-plus查询
        Page<Transfer> businessPage = page(page, wrapper);

        return businessPage;
    }
private final ClueMapper clueMapper;
    private final IClueService clueService;
    @Override
    @Transactional
    public ResponseResult change(Integer type, Long deptId, Long userId) {
        //查询所有者为用户ID和类型的 线索对象
        LambdaQueryWrapper<Clue> wrapper=Wrappers.<Clue>lambdaQuery()
                .eq(Clue::getOwner,userId)
                .eq(Clue::getStatus,type);
        List<Clue> list = clueMapper.selectList(wrapper);

        //更新每个对象的owner、assignBy和updateBy字段
        list.forEach(clue -> {
            clue.setOwner(deptId.toString());
            clue.setAssignBy(userId.toString());
            clue.setUpdateBy(userId.toString());
        });
        clueService.updateBatchById(list);
        return ResponseResult.success();
    }
}
